﻿using EntytiBookShop.DAL;
using EntytiBookShop.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EntytiBookShop
{
    public partial class AddAuthorForm : Form
    {
        DateTime birthDate;
        DateTime deathDate;
        string frstName;
        string lstName;
        string mdlName;
        string nickName;
        public AddAuthorForm()
        {

            InitializeComponent();

            DateTime dtMax = new DateTime(DateTime.Now.Year-18, DateTime.Now.Month, DateTime.Now.Day);
            dtpBirthDate.MaxDate = dtMax;
            DateTime dtMin = new DateTime(1800, 1, 1);
            dtpBirthDate.MinDate = dtMin;
            dtpDeathDate.MinDate = dtMin;
            dtpDeathDate.MaxDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);

            
        }

        private void dtpBirthDate_ValueChanged(object sender, EventArgs e)
        {
            birthDate = dtpBirthDate.Value.Date;
        }

        private void dtpDeathDate_ValueChanged(object sender, EventArgs e)
        {
            deathDate = dtpDeathDate.Value.Date;
        }

        private void tbLastName_TextChanged(object sender, EventArgs e)
        {
            lstName = tbLastName.Text;
        }

        private void tbFirstName_TextChanged(object sender, EventArgs e)
        {
            frstName = tbFirstName.Text;
        }

        private void tbMidleName_TextChanged(object sender, EventArgs e)
        {
            mdlName = tbMidleName.Text;
        }

        private void tbNickName_TextChanged(object sender, EventArgs e)
        {
            nickName = tbNickName.Text;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btAddAuthor_Click(object sender, EventArgs e)
        {
            if (frstName != null && lstName != null)
            {
                string message = "Add author?";
                string caption = "Author added succesfully";
                
                var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.No)
                {
                   
                }
                if (result == DialogResult.Yes)
                {
                    Author author = new Author();
                    author.FirstName = frstName;
                    author.LastName = lstName;
                    if (mdlName != null)
                    {
                        author.MiddleName = mdlName;
                    }
                    if (nickName != null)
                    {
                        author.NickName = nickName;
                    }
                    if (birthDate != null)
                    {
                        author.BirthDate = birthDate;
                    }
                    if (deathDate != null)
                    {
                        author.DeathDate = deathDate;
                    }
                    using (BookShopContext ctx = new BookShopContext())
                    {
                        ctx.Authors.Add(author);
                        ctx.SaveChanges();
                    }
                }
            }
        }
    }
}
